import router from '@ohos.router';
import display from '@ohos.display';
import prompt from '@system.prompt';
export default {
    data: {
        UpInterfaceTop:50,
        WordArray:[],
        DivIf:false,
        ButtonDisabked:false,
        UpInterfaceHeight:0,

        ScreenWidth:0,
        Screenheight:0,
        TotalWidth:100,
        Totalheight:30,
        ScreenWH:0,
        Y_Speed:0.22,
        ShootSpan:4000,
        Timer:60,
        NowClickWord:0,
        OptionClickTure:0,
        BackgroundPNG:'/common/image/闯关背景.png',
        BallonPNG:'/common/image/balloon2.png',
        OptionBackground:'/common/image/背景.png',
        Option1PNG:'/common/image/未选选项.png',
        Option2PNG:'/common/image/未选选项.png',
        Option3PNG:'/common/image/未选选项.png',
        Option4PNG:'/common/image/未选选项.png',
        Option5PNG:'/common/image/未选选项.png',
        Option1Disabled:false,
        Option2Disabled:false,
        Option3Disabled:false,
        Option4Disabled:false,
        Option5Disabled:false,
        SettlementDivShow:0,
        SettlementDivIf:false,
        OptionWordArray:[],
        StartTime:3,
        StartIf:true,
        TipIf:false,
        StartTimer:null,
        TotalTimer:null,
        YSpeedTimer:[],
        ScreenTimer:null,
        j:0,
        i:0,
        YSpeed:0.22,
        BallonClearNum:0,
        selectBalloonId:-1,
        unKnowId:0.4

    },
    onInit() {
    },
    back(){
        router.back()
    },
    onShow(){
        this.WordArray=setWordArray();
        //获取单词数组
        console.info('1--------------'+JSON.stringify(this.WordArray))
        let promise = display.getDefaultDisplay();
        //获取设备数据的promise对象
        promise.then((data) => {
            //链式
            console.info(JSON.stringify(data));
            //获取屏幕数据data
            this.ScreenWidth=data.width
            this.Screenheight=data.height
            //获取屏幕宽高
            this.ScreenWidth=1080
            this.Screenheight=2340
            //Proviewer无法获取数据，暂时利用本地数据代替
            this.ScreenWH=this.ScreenWidth/this.Screenheight
            //屏幕宽高比
            console.info('1111111111'+this.ScreenWH)
            this.WordArray=setRandom(this.WordArray,this.ScreenWH,this.Totalheight)
            //设置WordArray中关于气球大小位置正确单词选项之类随机值
            console.info('2--------------'+JSON.stringify(this.WordArray))
            this.Timer=((0.001*this.ShootSpan)*(this.WordArray.length))+((this.WordArray[this.WordArray.length-1].BallonRandowHeight*0.01*this.Totalheight+100)/(this.Y_Speed*60))
            //总计时器，闯关界面上方居中的那个，利用气球发射间隔和屏幕宽高计算总时间
            console.info('Timer iso'+this.Timer)
            this.Timer=Math.round(this.Timer)+5
            //给一个误差时间补偿
            console.info('Timer isp'+this.Timer)
            console.info('Timer num is'+((0.001*this.ShootSpan)*this.WordArray.length))
            console.info('ShootSpan is'+this.ShootSpan)
            console.info(this.WordArray.length)
            console.info('LastHeight is'+this.WordArray[this.WordArray.length-1].BallonRandowHeight)
            console.info('LastTimer is'+((this.WordArray[this.WordArray.length-1].BallonRandowHeight*0.01*this.Totalheight+100)/(this.Y_Speed*60)))
            //游戏开始
            this.GameStart()


        }).catch((err) => {
            console.info('getDefaultDisplay fail: ' + JSON.stringify(err));
        });
    },
    UpInterfaceTopset(){

    },
    GameStart(){
        this.j=0;
        //j为发射气球计数
        this.i=0;
        //i为各个气球上升动画对应数组索引
        this.YSpeedTimer=[]
        //用来存储气球上升计时器的数组
        this.BallonClearNum=0
        //已消失的气球数（暂时无用）
        console.info(this.WordArray.length)
        this.StartTimer=setInterval(()=>{
            //游戏开始倒计时
            if(this.StartTime=='GO!'){
                clearInterval(this.StartTimer)
                this.StartIf=false
            }
            else{
                if(this.StartTime==1){
                    this.StartTime='GO!'
                }
                else{
                    this.StartTime--
                }

            }
        },1000)

        TimerStart(this)
        //开启发射气球
    },
    BallonClick(e){
        this.selectBalloonId = e.target.attr.value-1
        TimerStop(this)
        //暂停游戏
        console.info('YJX8/28--------------------BallonClick')
        var BallonDialogDom=this.$element('dialog'+e.target.attr.value).show()
        //打开弹窗
        this.NowClickWord=e.target.attr.value-1
        //获取点击单词索引
        console.info('OptionWord----1---'+this.NowClickWord)
        console.info('OptionWord----2---'+this.WordArray[this.NowClickWord].RandomTrue)
        this.OptionWordArray.length=5
        this.OptionWordArray.fill('0')
        //选项单词译文数组，设置长度为5，并用’0‘充满
        console.info('OptionWord----3---'+JSON.stringify(this.OptionWordArray))

        this.OptionWordArray[0]=this.WordArray[this.NowClickWord].ErrorWord1
        this.OptionWordArray[1]=this.WordArray[this.NowClickWord].ErrorWord2
        this.OptionWordArray[2]=this.WordArray[this.NowClickWord].ErrorWord3
        //将错误译文按顺序填入
        console.info('OptionWord----4---'+JSON.stringify(this.OptionWordArray))
        //        var num=this.WordArray[this.NowClickWord].RandomTrue-1
        this.OptionWordArray.splice(this.WordArray[this.NowClickWord].RandomTrue-1,0,this.WordArray[this.NowClickWord].TrueWord)
        //将正确译文随机插入
        console.info('OptionWord----5---'+JSON.stringify(this.OptionWordArray))



    },
    confirmClick(e) {
        console.info('now dialog close')

//        this.$element(e.target.attr.id).close()
        //关闭对应弹窗dialog
        //初始化对应选项图片和是否禁用
        this.Option1PNG='/common/image/未选选项.png'
        this.Option2PNG='/common/image/未选选项.png'
        this.Option3PNG='/common/image/未选选项.png'
        this.Option4PNG='/common/image/未选选项.png'
        this.Option1Disabled=false
        this.Option2Disabled=false
        this.Option3Disabled=false
        this.Option4Disabled=false
        this.Option5Disabled=false
        //播放气球消失动画
        this.BallonClose()
    },
    OptionClick(e){
        //选项点击，正确选项随机，选种错误的显示正确选项，选中后选项框禁用
        var OptionId=e.target.attr.id
        console.info('OptionId------'+ OptionId)
        var IdNum
        //根据返回的选项id判断选中答案对错
        if(OptionId=='OptionOne'){
            console.info('OptionId if in ------')
            IdNum=1
            if(IdNum==this.WordArray[this.NowClickWord].RandomTrue){
                console.info('OptionId image change ------')
                this.Option1PNG='/common/image/正确.png'
                //禁用组件
                this.Option2Disabled=true
                this.Option3Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true

                //正确数++
                this.OptionClickTure++
            }
            else{
                this.Option1PNG='/common/image/错误.png'
                this.Option2Disabled=true
                this.Option3Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true

                //反之，选中错误的就将正确的显示出来
                if(this.WordArray[this.NowClickWord].RandomTrue==2){
                    this.Option2PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==3){
                    this.Option3PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==4){
                    this.Option4PNG='/common/image/正确.png'
                }
            }
        }
        else if(OptionId=='OptionTwo'){
            IdNum=2
            if(IdNum==this.WordArray[this.NowClickWord].RandomTrue){
                this.Option2PNG='/common/image/正确.png'
                this.Option1Disabled=true
                this.Option3Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true

                this.OptionClickTure++

            }
            else{
                this.Option2PNG='/common/image/错误.png'
                this.Option1Disabled=true
                this.Option3Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true


                if(this.WordArray[this.NowClickWord].RandomTrue==1){
                    this.Option1PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==3){
                    this.Option3PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==4){
                    this.Option4PNG='/common/image/正确.png'
                }


            }

        }
        else if(OptionId=='OptionThree'){
            IdNum=3
            if(IdNum==this.WordArray[this.NowClickWord].RandomTrue){
                this.Option3PNG='/common/image/正确.png'
                this.Option1Disabled=true
                this.Option2Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true

                this.OptionClickTure++

            }
            else{
                this.Option3PNG='/common/image/错误.png'
                this.Option1Disabled=true
                this.Option2Disabled=true
                this.Option4Disabled=true
                this.Option5Disabled=true


                if(this.WordArray[this.NowClickWord].RandomTrue==1){
                    this.Option1PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==2){
                    this.Option2PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==4){
                    this.Option4PNG='/common/image/正确.png'
                }


            }

        }
        else if(OptionId=='OptionFour'){
            IdNum=4
            if(IdNum==this.WordArray[this.NowClickWord].RandomTrue){
                this.Option4PNG='/common/image/正确.png'
                this.Option1Disabled=true
                this.Option2Disabled=true
                this.Option3Disabled=true
                this.Option5Disabled=true

                this.OptionClickTure++

            }
            else{
                this.Option4PNG='/common/image/错误.png'
                this.Option1Disabled=true
                this.Option2Disabled=true
                this.Option3Disabled=true
                this.Option5Disabled=true


                if(this.WordArray[this.NowClickWord].RandomTrue==1){
                    this.Option1PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==2){
                    this.Option2PNG='/common/image/正确.png'
                }
                else if(this.WordArray[this.NowClickWord].RandomTrue==3){
                    this.Option3PNG='/common/image/正确.png'
                }

            }

        }
        this.TipIf=true
        //这个是答题弹窗底部小贴士，目前作用为提醒玩家点击关闭弹窗


    },
    IncognizanceClick(e){
        if(this.unKnowId == this.WordArray.length){
            this.Timer = 0
        }
        this.unKnowId = e
        console.info('8/30YJX----- IncognizanceClick on')
        this.Option1Disabled=true
        this.Option2Disabled=true
        this.Option3Disabled=true
        this.Option4Disabled=true
        this.Option5Disabled=true
        this.Option5PNG='/common/image/正确.png'
        if(this.WordArray[this.NowClickWord].RandomTrue==1){
            this.Option1PNG='/common/image/正确.png'
        }
        else if(this.WordArray[this.NowClickWord].RandomTrue==2){
            this.Option2PNG='/common/image/正确.png'
        }
        else if(this.WordArray[this.NowClickWord].RandomTrue==3){
            this.Option3PNG='/common/image/正确.png'
        }
        else if(this.WordArray[this.NowClickWord].RandomTrue==4){
            this.Option4PNG='/common/image/正确.png'
        }
        this.TipIf=true

    },
    BallonClose(idx){
        var CloseSpeed=0.1
        //气球消失动画，利用能见度
        var BallonCloseTimer=setInterval(()=>{
            if(this.WordArray[this.NowClickWord].BallonIf<=0){
                console.info('now BallonClose')
                clearInterval(BallonCloseTimer)
            }
            this.WordArray[this.NowClickWord].BallonIf=this.WordArray[this.NowClickWord].BallonIf-CloseSpeed
            console.info('now '+this.NowClickWord+' BallonIf is--'+this.WordArray[this.NowClickWord].BallonIf)
        },1000/60)
        TimerStart(this)
        //关闭弹窗后继续计时，游戏继续
        console.info('8/29YJX-----Close j'+this.j)
        console.info('8/29YJX-----Close Timer'+this.Timer)
        this.TipIf=false
        //关闭小贴士


    },
}
function     TimerCount(j,i,YSpeedTimer,TotalTimer,ScreenTimer,Indexthis){
    //顶部计时器
    console.info('8/29YJX-----this.Timer'+Indexthis.Timer)
    if(Indexthis.Timer==0){
        //时间为零时清除上升计时器和发射计时器
        console.info('Timer clear start')
        console.info('Time j is--'+j)
        console.info('Time i is--'+i)
        console.info('Time YSpeedTimer length is---'+YSpeedTimer.length)
        for(var num=0;num<j;num++){
            console.info('Timer clear---'+num)
            clearInterval(YSpeedTimer[num])
        }
        clearInterval(TotalTimer)
        console.info('Time Out GameOver')
        Indexthis.SettlementDivIf=true
        //触发结算弹窗
        //        Indexthis.OptionClickTure=Math.round(Indexthis.OptionClickTure*5/30)
        var CloseSpeed=0.1
        var BallonCloseTimer=setInterval(()=>{
            if(Indexthis.WordArray[Indexthis.NowClickWord].BallonIf<=0){
                console.info('now BallonClose')
                clearInterval(BallonCloseTimer)
            }
            Indexthis.WordArray[Indexthis.NowClickWord].BallonIf=Indexthis.WordArray[Indexthis.NowClickWord].BallonIf-CloseSpeed
            console.info('now '+Indexthis.NowClickWord+' BallonIf is--'+Indexthis.WordArray[Indexthis.NowClickWord].BallonIf)
        },1000/60)

        var SettlementDivShowingTimer=setInterval(()=>{
            //结算弹窗动画，利用透明度0-》1
            if(Indexthis.SettlementDivShow>=1){
                console.info('Show SettlementDivShowingTimer clear')
                clearInterval(SettlementDivShowingTimer)
                //大于1触发清除计时器
            }
            console.info('Show is ---'+Indexthis.SettlementDivShow)
            Indexthis.SettlementDivShow=Indexthis.SettlementDivShow+0.05
            //每帧增加0.05
        },1000/60)
        clearInterval(ScreenTimer)
        //最后清除顶部计时器

    }else{
        //否则计数Timer--
        Indexthis.Timer--

    }
    console.info('Timer--is '+Indexthis.Timer)
}
function    YSpeedTimerCount(i,Indexthis){
    //气球上升计时器函数
    var x=i
    //获取输入参数
    Indexthis.YSpeedTimer[x]=setInterval(()=>{
        //气球上升计时器，
        if(i>=Indexthis.WordArray.length){

            clearInterval(Indexthis.YSpeedTimer[i])
        }
        if(Indexthis.SettlementDivIf>=1){
            clearInterval(Indexthis.YSpeedTimer[i])
        }
        console.info('8/29-----Yspeed i is---'+i)
        Indexthis.WordArray[i].BallonTop-=Indexthis.YSpeed
        //减少气球距离顶部距离
        var InterfaceDom=Indexthis.$element(i+1).getBoundingClientRect()
        //获取气球与顶部距离，自身高度等参数
        if(InterfaceDom.top<-InterfaceDom.height){
            //判断并清除
            clearInterval(Indexthis.YSpeedTimer[i])
            console.info('第'+i+'个气球已发射完毕')
            Indexthis.BallonClearNum++
        }

    },1000/60)

}
function  TotalTimercount(j,i,Indexthis){
    //气球发射计时器
    console.info('8/29YJX-----Start aaa---'+Indexthis.WordArray.length)
    if(j>=Indexthis.WordArray.length){
        clearInterval(Indexthis.TotalTimer)
        console.info('8/29YJX-----clear')
    }
    console.info('第'+j+'个气球开始发射')
    i=j

    if(j==0){
        //第一个气球发射时开始计时
        Indexthis.ScreenTimer=setInterval(()=>{
            TimerCount(j,i,Indexthis.YSpeedTimer,Indexthis.TotalTimer,Indexthis.ScreenTimer,Indexthis)
        },1000)
    }
    console.info('8/29YJX-----'+i)
    YSpeedTimerCount(i,Indexthis)
    //发射对应气球
    console.info('自加一次')
    j++
    return j
    //自加并返回
}
function TimerStop(Indexthis){
    //暂停原理为清除所有计时器
    clearInterval(Indexthis.TotalTimer)
    Indexthis.TotalTimer=null
    clearInterval(Indexthis.ScreenTimer)
    Indexthis.ScreenTimer=null
    for(var i=0;i<Indexthis.j;i++){
        clearInterval(Indexthis.YSpeedTimer[i])
        Indexthis.YSpeedTimer[i]=null
    }
}
function TimerStart(Indexthis){
    //开始计时函数
    if(Indexthis.TotalTimer==null){
        //        TotalTimerCount(Indexthis.j,YSpeed,BallonClearNum,i)
        console.info('8/29YJX-----Start Now J is---'+Indexthis.j)
        console.info('8/29YJX-----Start ShootSpan---- '+Indexthis.ShootSpan)
        Indexthis.TotalTimer=setInterval(()=>{
            //开始发射气球
            Indexthis.j=TotalTimercount(Indexthis.j,Indexthis.i,Indexthis)
            console.info('8/29YJX-----Start J++'+Indexthis.j)
        },Indexthis.ShootSpan)
        if(Indexthis.j!=0){
            //当情况为暂停后继续发射时，则计时器继续计时
            Indexthis.ScreenTimer=setInterval(()=>{
                TimerCount(Indexthis.j,Indexthis.i,Indexthis.YSpeedTimer,Indexthis.TotalTimer,Indexthis.ScreenTimer,Indexthis)
            },1000)
        }
    }

    for(let num=0;num<Indexthis.j;num++){
        //需使已发射的气球继续上升至脱离屏幕
        if(Indexthis.YSpeedTimer[num]==null){
            if(num==0){
                Indexthis.i=0
            }
            YSpeedTimerCount(num,Indexthis)
            console.info('8/29YJX----reBallonUpTimer---'+Indexthis.i)
        }
    }
}
function setWordArray(){
    var WordArray=[
        {
            BallonRandowWidth:10,
            BallonRandowHeight:10,
            BallonRandowLeft:10,
            BallonLeftMoveSpeed:0,
            BallonTop:100,
            BallonIf:1,
            SizeAdapt:0,
            RandomTrue:0,
            Word:'pen',
            TrueWord:'n.钢笔',
            ErrorWord1:'n.尺子',
            ErrorWord2:'n.盘子',
            ErrorWord3:'n.橡皮',



        },
        {
            BallonRandowWidth:10,
            BallonRandowHeight:10,
            BallonRandowLeft:10,
            BallonLeftMoveSpeed:0,
            BallonTop:100,
            BallonIf:1,
            SizeAdapt:0,
            RandomTrue:0,
            Word:'newspaper',
            TrueWord:'n.报纸',
            ErrorWord1:'n.杂志',
            ErrorWord2:'n.新闻',
            ErrorWord3:'n.纸张',


        },
        {
            BallonRandowWidth:10,
            BallonRandowHeight:10,
            BallonRandowLeft:10,
            BallonLeftMoveSpeed:0,
            BallonTop:100,
            BallonIf:1,
            SizeAdapt:0,
            RandomTrue:0,
            Word:'book',
            TrueWord:'n.书本',
            ErrorWord1:'n.科目',
            ErrorWord2:'n.尺子',
            ErrorWord3:'n.钢笔',


        },
        {
            BallonRandowWidth:10,
            BallonRandowHeight:10,
            BallonRandowLeft:10,
            BallonLeftMoveSpeed:0,
            BallonTop:100,
            BallonIf:1,
            SizeAdapt:0,
            RandomTrue:0,
            Word:'post card',
            TrueWord:'n.明信片',
            ErrorWord1:'n.邮件',
            ErrorWord2:'n.圆珠笔',
            ErrorWord3:'n.校园卡',


        },
        {
            BallonRandowWidth:10,
            BallonRandowHeight:10,
            BallonRandowLeft:10,
            BallonLeftMoveSpeed:0,
            BallonTop:100,
            BallonIf:1,
            SizeAdapt:0,
            RandomTrue:0,
            Word:'sharpener',
            TrueWord:'n.削笔刀',
            ErrorWord1:'n.蜡笔',
            ErrorWord2:'n.刀子',
            ErrorWord3:'n.书包',

        },


    ]
    return WordArray
}
function getRndInteger(min, max) {//得到范围内随机数（不包括左右边界
    return Math.floor(Math.random() * (max - min) ) + min;
}
function setRandom(WordArray,ScreenWH,Totalheight){
    for (var i = 0; i < WordArray.length; i++) {
        console.info('8/30YJX----'+WordArray[i].Word.length)
        let Length=WordArray[i].Word.length
        if(Length<=5){
            WordArray[i].BallonRandowWidth=getRndInteger(30,40)
        }
        else if(Length>5&&Length<=15){
            WordArray[i].BallonRandowWidth=getRndInteger(55,65)
        }
        else{
            WordArray[i].BallonRandowWidth=65
        }
        console.info(i+'Random----  '+WordArray[i].BallonRandowWidth)
        //高度获取利用宽度，图形宽高比，屏幕宽高比
        console.info(i+'Random----ScreenWH  '+ScreenWH)

        WordArray[i].BallonRandowHeight=(WordArray[i].BallonRandowWidth*1.2146*ScreenWH*100)/Totalheight

        console.info(i+'Random----  '+WordArray[i].BallonRandowHeight)
        //var brl=ScreenWidth-WordArray[i].BallonRandowWidth
        WordArray[i].BallonRandowLeft=getRndInteger(0, 100-WordArray[i].BallonRandowWidth)
        console.info(i+'Random----  '+WordArray[i].BallonRandowLeft)
        WordArray[i].RandomTrue=Math.trunc(getRndInteger(1,5))
        //去除小数取整，随机1.2.3选项为正确
        console.info(i+'Random----  '+WordArray[i].RandomTrue)

        //WordArray[i].SizeAdapt=
        let sign= Math.random() > 0.5 ? '-' : '';
        WordArray[i].BallonLeftMoveSpeed=parseInt(sign+getRndInteger(1,3))
    }
    return WordArray
}
function Ballonmove(WordArrayi){
    var Ballontimer=setInterval(()=>{
        WordArrayi.BallonRandowLeft+=WordArrayi.BallonLeftMoveSpeed
    },1000/60)

}
